package net.ssta.util.bill.chargeobj;

import java.sql.Connection;
import java.sql.SQLException;


import org.apache.log4j.Logger;

import net.ssta.common.Constants;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;

/**
 * 此类用于计算科技成果，机场，专家的扣费数目
 * 第一次进系统时初始化
 * 其他时候，在修改值之后再重新取一次值
 * @author Administrator
 *
 */
public class CostNumberConstants {
	private static Logger log = Logger.getLogger(CostNumberConstants.class.getClass()); 
	public static int kjcg;
	public static int unit;
	public static int expert;
	
	
	public static void initCostNumber(){
		Connection conn=null;
		try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql="SELECT * FROM T_COSTNUM";
	    	RecordSet rs=dbe.find(sql);
	    	if(rs!=null&&rs.size()>0){
	    		kjcg=rs.get(0).getInt("KJCG");
	    		unit=rs.get(0).getInt("UNIT");
	    		expert=rs.get(0).getInt("EXPERT");
	    	}else{
	    		kjcg=0;
	    		unit=0;
	    		expert=0;
	    	}
		} catch (SQLException e) {
			log.error("获取科研机构的扣除金额出错："+e);
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		
	}
	
	
	/**
	 * 更新积分
	 * @param _kjcg
	 * @param _unit
	 * @param _expert
	 */
	public static void updateCostNumber(int _kjcg,int _unit,int _expert){
		Connection conn=null;
		try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql="SELECT * FROM T_COSTNUM";
	    	RecordSet rs=dbe.find(sql);
	    	if(rs==null||rs.size()<1){
	    		Record r=new Record();
	    		r.setConnection(conn);
	    		r.setEntityName("T_COSTNUM");
	    		r.addData("KJCG",_kjcg);
	    		r.addData("UNIT",_unit);
	    		r.addData("EXPERT",_expert);
	    		r.setPrimaryKey("COSTID");
	    		r.create();
	    	}else{
	    		sql=" update T_COSTNUM set kjcg="+_kjcg+",unit="+_unit+",expert="+_expert;
	    		dbe.execute(sql);
	    	}
	    	initCostNumber();
		} catch (SQLException e) {
			log.error("获取科研机构的扣除金额出错："+e);
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		
	}
	
	static{
		initCostNumber();
	}

	 

	/**
	 * @return the kjcg
	 */
	public static int getKjcg() {
		return kjcg;
	}


	/**
	 * @return the unit
	 */
	public static int getUnit() {
		return unit;
	}
	/**
	 * @return the unit
	 */
	public static int getExpert() {
		return expert;
	}
 
	
}
